package fgame.action
{
	import fgame.GameState;
	import fgame.unit.Actor3D;
	
	import flare.collisions.CollisionInfo;
	
	import flash.geom.Vector3D;
	
	/**
	 * 客户端的操作
	 */
	public class ControllAct extends Act3D
	{
		public function ControllAct(actor:Actor3D=null)
		{
			super(actor);
			_types=3;
			_animProperty="walk";
		}
		
		
		override public function begin():void
		{
			if(!_driving) //如果已经处于控制中部需要再次begin
			{
				super.begin();
				 
				this.currActor.playAnimAct(animProperty);
	
				GameState.inst.sceneManager.addCollisionWithObstacle(currActor.sphereCollision);
			}
			_driving=true;
		}
		
		override public function end():void
		{
			GameState.inst.sceneManager.removeCollisionWithObstacle(currActor.sphereCollision);
			
			currActor.playAnimAct("idle");
			
			_driving=false;//控制结束
			
			Debug.traceMe("ControllAct...");
			
			super.end();

		}
		
		override public function purpose(args:*):void
		{
			UP=args["a"];
			DOWN=args["b"];
			LEFT=args["c"];
			RIGHT=args["d"];
			
			_rayCollisions=GameState.inst.sceneManager.rayCollision;
			if(UP==false && DOWN==false && LEFT==false && RIGHT==false)
				this.complete();
		}
		
		override public function action(time:Number):void
		{
			Debug.traceMe("ControllAct...");
			if(UP)
				speed -= 0.2
					
			if(DOWN)
				speed += 0.2;
			
			if(LEFT)
				direction += 6;
			
			if(RIGHT)
				direction -= 6;

					
			speed *= 0.9;
			direction *= 0.9;
			
			var roy:Number= direction * speed / 20;
			
			this.graphic.translateZ( speed );
			this.graphic.rotateY(roy);
			
			var from:Vector3D = graphic.localToGlobal( new Vector3D( 0, 50, 0 ) );
			var dir:Vector3D = graphic.getDown();
			
			var info:CollisionInfo;
			if(rayCollisions.test(from,dir))
			{
				info = rayCollisions.data[0];
				
				graphic.setPosition( info.point.x, info.point.y, info.point.z );

				graphic.setNormalOrientation( info.normal, 0.05 );
				
			}
			
			currActor.sphereCollision.slider();
		}

		private var speed:Number=0,direction:Number=0;
		private var _driving:Boolean=false;
		private var UP:Boolean,DOWN:Boolean,LEFT:Boolean,RIGHT:Boolean;

	}
}